package com.phkj.qdh.ui.fragment.failover

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.FailoverReduceType
import com.phkj.qdh.bean.FailoverTypeBean
import com.phkj.qdh.bean.ZheDiThing
import com.phkj.qdh.net.code
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.FailoverRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion

class FailoverVM(val res: FailoverRepository) : BaseVM() {
    private val _failoverTypeList = MutableSharedFlow<ArrayList<FailoverTypeBean>>()
    val failoverTypeList get() = _failoverTypeList

    fun getFailoverTypeList(map: HashMap<String, Any?>) {
        requestIO {
            res.getFailoverTypeList(map).catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _failoverTypeList.emit(it)
            }
        }

    }


    private val _failoverSaveResult = MutableSharedFlow<Int>()
    val failoverSaveResult get() = _failoverSaveResult

    fun saveFailover(map: HashMap<String, Any?>) {
        requestIO {
            res.saveFailover(map).catch { cause ->
                ToastUtils.show(cause.msg)
                _failoverSaveResult.emit(cause.code)
            }.collectLatest {

            }
        }

    }


    private val _failoverReduceTypeList = MutableSharedFlow<ArrayList<FailoverReduceType>>()
    val failoverReduceTypeList get() = _failoverReduceTypeList
    fun getFailoverReduceType() {
        requestIO {
            res.getFailoverReduceType().catch { cause ->
                ToastUtils.show(cause.msg)
            }.collectLatest {
                _failoverReduceTypeList.emit(it)
            }
        }

    }


    private val _failoverReduceThingList = MutableSharedFlow<ArrayList<ZheDiThing>>()
    val failoverReduceThingList get() = _failoverReduceThingList
    fun getFailoverReduceThings(map: HashMap<String, Any?>) {
        requestIO {
            res.getFailoverReduceThings(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(0)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _failoverReduceThingList.emit(it.data)
                stop(0)
            }
        }

    }


    private val _failoverReduceDeductions = MutableSharedFlow<ArrayList<ZheDiThing>>()
    val failoverReduceDeductions get() = _failoverReduceDeductions
    fun getFailoverDeductions(map: HashMap<String, Any?>) {
        requestIO {
            res.getFailoverReduceTypeThings(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(0)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _failoverReduceDeductions.emit(it.data)
                stop(0)
            }
        }

    }


    private val _state = MutableSharedFlow<Int>()
    val state get() = _state

    private suspend fun stop(i: Int) {
        _state.emit(i)
    }


}


fun getFailoverVMProvider() = FailoverVMProvider(FailoverRepository)


class FailoverVMProvider(val res: FailoverRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return FailoverVM(res) as T
    }
}
